BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.AccessMask.Board_Id', N'Tmp_BoardId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.AccessMask.Tmp_BoardId', N'BoardId', 'COLUMN' 
GO

ALTER TABLE dbo.AccessMask SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.Category.Board_Id', N'Tmp_BoardId_1', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Category.Tmp_BoardId_1', N'BoardId', 'COLUMN' 
GO

ALTER TABLE dbo.Category SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.Attachment.Author_Id', N'Tmp_AuthorId_2', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Attachment.Post_Id', N'Tmp_PostId_3', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Attachment.Tmp_AuthorId_2', N'AuthorId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Attachment.Tmp_PostId_3', N'PostId', 'COLUMN' 
GO

ALTER TABLE dbo.Attachment SET (LOCK_ESCALATION = TABLE)
GO

COMMIT


BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.Board.SiteDescription', N'Tmp_Description_4', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Board.Tmp_Description_4', N'Description', 'COLUMN' 
GO

ALTER TABLE dbo.Board
	DROP COLUMN SiteName, DefaultLanguage, DefaultTimeZone, DefaultDateTimeFormat, Theme, AllowUserTheme
GO

ALTER TABLE dbo.Board SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.Forum.ParentForum_Id', N'Tmp_ParentForumId_5', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.LastTopic_Id', N'Tmp_LastTopicId_6', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.LastPost_Id', N'Tmp_LastPostId_7', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.LastPostUser_Id', N'Tmp_LastPostUserId_8', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.Category_Id', N'Tmp_CategoryId_9', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.Tmp_ParentForumId_5', N'ParentForumId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.Tmp_LastTopicId_6', N'LastTopicId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.Tmp_LastPostId_7', N'LastPostId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.Tmp_LastPostUserId_8', N'LastPostUserId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Forum.Tmp_CategoryId_9', N'CategoryId', 'COLUMN' 
GO

ALTER TABLE dbo.Forum SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.ForumAccess.Forum_Id', N'Tmp_ForumId_10', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumAccess.Group_Id', N'Tmp_GroupId_11', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumAccess.AccessMask_Id', N'Tmp_AccessMaskId_12', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumAccess.Tmp_ForumId_10', N'ForumId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumAccess.Tmp_GroupId_11', N'GroupId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumAccess.Tmp_AccessMaskId_12', N'AccessMaskId', 'COLUMN' 
GO

ALTER TABLE dbo.ForumAccess SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.FollowForum.User_Id', N'Tmp_UserId_13', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.FollowForum.Forum_Id', N'Tmp_ForumId_14', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.FollowForum.Tmp_UserId_13', N'ForumUserId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.FollowForum.Tmp_ForumId_14', N'ForumId', 'COLUMN' 
GO

ALTER TABLE dbo.FollowForum SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.FollowTopic.User_Id', N'Tmp_UserId_15', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.FollowTopic.Topic_Id', N'Tmp_TopicId_16', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.FollowTopic.Tmp_UserId_15', N'ForumUserId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.FollowTopic.Tmp_TopicId_16', N'TopicId', 'COLUMN' 
GO

ALTER TABLE dbo.FollowTopic SET (LOCK_ESCALATION = TABLE)
GO

COMMIT


BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.ForumTrack.Forum_Id', N'Tmp_ForumId_17', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumTrack.User_Id', N'Tmp_ForumUserId_18', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumTrack.Tmp_ForumId_17', N'ForumId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumTrack.Tmp_ForumUserId_18', N'ForumUserId', 'COLUMN' 
GO

ALTER TABLE dbo.ForumTrack SET (LOCK_ESCALATION = TABLE)
GO

COMMIT


BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.ForumUser.UserFlagValue', N'Tmp_UserFlag_19', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.ForumUser.Tmp_UserFlag_19', N'UserFlag', 'COLUMN' 
GO

ALTER TABLE dbo.ForumUser SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.GroupMember.Group_Id', N'Tmp_GroupId_20', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.GroupMember.ForumUser_Id', N'Tmp_ForumUserId_21', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.GroupMember.Tmp_GroupId_20', N'GroupId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.GroupMember.Tmp_ForumUserId_21', N'ForumUserId', 'COLUMN' 
GO

ALTER TABLE dbo.GroupMember SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.PostReport.Post_Id', N'Tmp_PostId_22', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.PostReport.ReportedBy_Id', N'Tmp_ReportedById_23', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.PostReport.ResolvedBy_Id', N'Tmp_ResolvedById_24', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.PostReport.Tmp_PostId_22', N'PostId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.PostReport.Tmp_ReportedById_23', N'ReportedById', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.PostReport.Tmp_ResolvedById_24', N'ResolvedById', 'COLUMN' 
GO

ALTER TABLE dbo.PostReport SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.Topic.TypeValue', N'Tmp_Type_27', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.LastPost_Id', N'Tmp_LastPostId_28', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.LastPostAuthor_Id', N'Tmp_LastPostAuthorId_29', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Author_Id', N'Tmp_AuthorId_30', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Forum_Id', N'Tmp_ForumId_31', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Tmp_Type_27', N'Type', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Tmp_LastPostId_28', N'LastPostId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Tmp_LastPostAuthorId_29', N'LastPostAuthorId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Tmp_AuthorId_30', N'AuthorId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.Topic.Tmp_ForumId_31', N'ForumId', 'COLUMN' 
GO

ALTER TABLE dbo.Topic ADD
	OriginalTopicId int NULL
GO

ALTER TABLE dbo.Topic SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

EXECUTE sp_rename N'dbo.TopicTrack.Topic_Id', N'Tmp_TopicId_32', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.TopicTrack.User_Id', N'Tmp_UserId_33', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.TopicTrack.Tmp_TopicId_32', N'TopicId', 'COLUMN' 
GO

EXECUTE sp_rename N'dbo.TopicTrack.Tmp_UserId_33', N'ForumUserId', 'COLUMN' 
GO

ALTER TABLE dbo.TopicTrack SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.[User] ADD
	Approved bit NULL,
	LastPasswordFailure datetime NULL,
	PasswordFailures int NULL,
	LastLockout datetime NULL
GO

ALTER TABLE dbo.[User] SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

UPDATE dbo.[User] SET Approved = ~Locked, LastPasswordFailure = CONVERT(datetime, '1970-1-1'), PasswordFailures = 0, LastLockout = CONVERT(datetime, '1970-1-1');
GO

UPDATE dbo.[User] SET Locked = 0;
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.ForumUser ADD
	Active bit NULL
GO

ALTER TABLE dbo.ForumUser SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

UPDATE dbo.[ForumUser] SET Active = 1;
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.[User]
	DROP CONSTRAINT DF__User__Id__2E1BDC42
GO

CREATE TABLE dbo.Tmp_User
	(
	Id uniqueidentifier NOT NULL,
	Username nvarchar(256) NOT NULL,
	EmailAddress nvarchar(200) NOT NULL,
	Password nvarchar(100) NOT NULL,
	Created datetime NOT NULL,
	LastVisit datetime NOT NULL,
	Locked bit NOT NULL,
	Approved bit NOT NULL,
	LastPasswordFailure datetime NOT NULL,
	PasswordFailures int NOT NULL,
	LastLockout datetime NOT NULL
	)  ON [PRIMARY]
GO

ALTER TABLE dbo.Tmp_User SET (LOCK_ESCALATION = TABLE)
GO

ALTER TABLE dbo.Tmp_User ADD CONSTRAINT
	DF__User__Id__2E1BDC42 DEFAULT (newid()) FOR Id
GO

IF EXISTS(SELECT * FROM dbo.[User])
	 EXEC('INSERT INTO dbo.Tmp_User (Id, Username, EmailAddress, Password, Created, LastVisit, Locked, Approved, LastPasswordFailure, PasswordFailures, LastLockout)
		SELECT Id, Username, EmailAddress, Password, Created, LastVisit, Locked, Approved, LastPasswordFailure, PasswordFailures, LastLockout FROM dbo.[User] WITH (HOLDLOCK TABLOCKX)')
GO

ALTER TABLE dbo.Headline
	DROP CONSTRAINT [FK_dbo.Headline_dbo.User_User_Id]
GO

ALTER TABLE dbo.RoleUser
	DROP CONSTRAINT [FK_dbo.RoleUser_dbo.User_User_Id]
GO

DROP TABLE dbo.[User]
GO

EXECUTE sp_rename N'dbo.Tmp_User', N'User', 'OBJECT' 
GO

COMMIT

BEGIN TRANSACTION
GO

CREATE TABLE dbo.Tmp_ForumUser
	(
	Id int NOT NULL IDENTITY (1, 1),
	ProviderId nvarchar(50) NOT NULL,
	Name nvarchar(256) NOT NULL,
	EmailAddress nvarchar(200) NOT NULL,
	FirstVisit datetime NOT NULL,
	LastVisit datetime NOT NULL,
	LastIP nvarchar(50) NULL,
	Deleted bit NOT NULL,
	Timezone nvarchar(100) NOT NULL,
	Culture nvarchar(10) NOT NULL,
	FullName nvarchar(200) NULL,
	UserFlag int NOT NULL,
	UseFullName bit NOT NULL,
	Active bit NOT NULL
	)  ON [PRIMARY]
GO

ALTER TABLE dbo.Tmp_ForumUser SET (LOCK_ESCALATION = TABLE)
GO

SET IDENTITY_INSERT dbo.Tmp_ForumUser ON
GO

IF EXISTS(SELECT * FROM dbo.ForumUser)
	 EXEC('INSERT INTO dbo.Tmp_ForumUser (Id, ProviderId, Name, EmailAddress, FirstVisit, LastVisit, LastIP, Deleted, Timezone, Culture, FullName, UserFlag, UseFullName, Active)
		SELECT Id, ProviderId, Name, EmailAddress, FirstVisit, LastVisit, LastIP, Deleted, Timezone, Culture, FullName, UserFlag, UseFullName, Active FROM dbo.ForumUser WITH (HOLDLOCK TABLOCKX)')
GO

SET IDENTITY_INSERT dbo.Tmp_ForumUser OFF
GO

ALTER TABLE dbo.Attachment
	DROP CONSTRAINT [FK_dbo.Attachment_dbo.ForumUser_Author_Id]
GO

ALTER TABLE dbo.FollowForum
	DROP CONSTRAINT [FK_dbo.FollowForum_dbo.ForumUser_User_Id]
GO

ALTER TABLE dbo.FollowTopic
	DROP CONSTRAINT [FK_dbo.FollowTopic_dbo.ForumUser_User_Id]
GO

ALTER TABLE dbo.Forum
	DROP CONSTRAINT [FK_dbo.Forum_dbo.ForumUser_LastPostUser_Id]
GO

ALTER TABLE dbo.ForumTrack
	DROP CONSTRAINT [FK_dbo.ForumTrack_dbo.ForumUser_User_Id]
GO

ALTER TABLE dbo.GroupMember
	DROP CONSTRAINT [FK_dbo.GroupMember_dbo.ForumUser_ForumUser_Id]
GO

ALTER TABLE dbo.Post
	DROP CONSTRAINT [FK_dbo.Post_dbo.ForumUser_Author_Id]
GO

ALTER TABLE dbo.PostReport
	DROP CONSTRAINT [FK_dbo.PostReport_dbo.ForumUser_ReportedBy_Id]
GO

ALTER TABLE dbo.PostReport
	DROP CONSTRAINT [FK_dbo.PostReport_dbo.ForumUser_ResolvedBy_Id]
GO

ALTER TABLE dbo.Topic
	DROP CONSTRAINT [FK_dbo.Topic_dbo.ForumUser_Author_Id]
GO

ALTER TABLE dbo.Topic
	DROP CONSTRAINT [FK_dbo.Topic_dbo.ForumUser_LastPostAuthor_Id]
GO

ALTER TABLE dbo.TopicTrack
	DROP CONSTRAINT [FK_dbo.TopicTrack_dbo.ForumUser_User_Id]
GO

DROP TABLE dbo.ForumUser
GO

EXECUTE sp_rename N'dbo.Tmp_ForumUser', N'ForumUser', 'OBJECT' 
GO

ALTER TABLE dbo.ForumUser ADD CONSTRAINT
	[PK_dbo.ForumUser] PRIMARY KEY CLUSTERED 
	(
	Id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.TopicTrack ADD CONSTRAINT
	[FK_dbo.TopicTrack_dbo.ForumUser_User_Id] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.TopicTrack SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.Topic ADD CONSTRAINT
	[FK_dbo.Topic_dbo.ForumUser_Author_Id] FOREIGN KEY
	(
	AuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.Topic ADD CONSTRAINT
	[FK_dbo.Topic_dbo.ForumUser_LastPostAuthor_Id] FOREIGN KEY
	(
	LastPostAuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.Topic SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.PostReport ADD CONSTRAINT
	[FK_dbo.PostReport_dbo.ForumUser_ReportedBy_Id] FOREIGN KEY
	(
	ReportedById
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.PostReport ADD CONSTRAINT
	[FK_dbo.PostReport_dbo.ForumUser_ResolvedBy_Id] FOREIGN KEY
	(
	ResolvedById
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.PostReport SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO
EXECUTE sp_rename N'dbo.Post.ReplyToPost_Id', N'Tmp_ReplyToPostId', 'COLUMN' 
GO
EXECUTE sp_rename N'dbo.Post.Author_Id', N'Tmp_AuthorId_1', 'COLUMN' 
GO
EXECUTE sp_rename N'dbo.Post.Topic_Id', N'Tmp_TopicId_2', 'COLUMN' 
GO
EXECUTE sp_rename N'dbo.Post.Tmp_ReplyToPostId', N'ReplyToPostId', 'COLUMN' 
GO
EXECUTE sp_rename N'dbo.Post.Tmp_AuthorId_1', N'AuthorId', 'COLUMN' 
GO
EXECUTE sp_rename N'dbo.Post.Tmp_TopicId_2', N'TopicId', 'COLUMN' 
GO
ALTER TABLE dbo.Post SET (LOCK_ESCALATION = TABLE)
GO
COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.Post ADD CONSTRAINT
	[FK_dbo.Post_dbo.ForumUser_Author_Id] FOREIGN KEY
	(
	AuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.Post SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.GroupMember ADD CONSTRAINT
	[FK_dbo.GroupMember_dbo.ForumUser_ForumUser_Id] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.GroupMember SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.ForumTrack ADD CONSTRAINT
	[FK_dbo.ForumTrack_dbo.ForumUser_User_Id] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.ForumTrack SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.Forum ADD CONSTRAINT
	[FK_dbo.Forum_dbo.ForumUser_LastPostUser_Id] FOREIGN KEY
	(
	LastPostUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.Forum SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.FollowTopic ADD CONSTRAINT
	[FK_dbo.FollowTopic_dbo.ForumUser_User_Id] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.FollowTopic SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.FollowForum ADD CONSTRAINT
	[FK_dbo.FollowForum_dbo.ForumUser_User_Id] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.FollowForum SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.Attachment ADD CONSTRAINT
	[FK_dbo.Attachment_dbo.ForumUser_Author_Id] FOREIGN KEY
	(
	AuthorId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.Attachment SET (LOCK_ESCALATION = TABLE)
GO

COMMIT


BEGIN TRANSACTION
GO

ALTER TABLE dbo.TopicTrack
	DROP CONSTRAINT [FK_dbo.TopicTrack_dbo.Topic_Topic_Id]
GO

ALTER TABLE dbo.Topic SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.TopicTrack
	DROP CONSTRAINT [FK_dbo.TopicTrack_dbo.ForumUser_User_Id]
GO

ALTER TABLE dbo.ForumUser SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

BEGIN TRANSACTION
GO

CREATE TABLE dbo.Tmp_TopicTrack
	(
	Id int NOT NULL IDENTITY (1, 1),
	LastViewed datetime NOT NULL,
	TopicId int NOT NULL,
	ForumUserId int NOT NULL
	)  ON [PRIMARY]
GO

ALTER TABLE dbo.Tmp_TopicTrack SET (LOCK_ESCALATION = TABLE)
GO

SET IDENTITY_INSERT dbo.Tmp_TopicTrack ON
GO

IF EXISTS(SELECT * FROM dbo.TopicTrack)
	 EXEC('INSERT INTO dbo.Tmp_TopicTrack (Id, LastViewed, TopicId, ForumUserId)
		SELECT Id, CONVERT(datetime, LastViewed), TopicId, ForumUserId FROM dbo.TopicTrack WITH (HOLDLOCK TABLOCKX)')
GO

SET IDENTITY_INSERT dbo.Tmp_TopicTrack OFF
GO

DROP TABLE dbo.TopicTrack
GO

EXECUTE sp_rename N'dbo.Tmp_TopicTrack', N'TopicTrack', 'OBJECT' 
GO

ALTER TABLE dbo.TopicTrack ADD CONSTRAINT
	[PK_dbo.TopicTrack] PRIMARY KEY CLUSTERED 
	(
	Id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

CREATE NONCLUSTERED INDEX IX_Topic_Id ON dbo.TopicTrack
	(
	TopicId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX IX_User_Id ON dbo.TopicTrack
	(
	ForumUserId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

ALTER TABLE dbo.TopicTrack ADD CONSTRAINT
	[FK_dbo.TopicTrack_dbo.ForumUser_User_Id] FOREIGN KEY
	(
	ForumUserId
	) REFERENCES dbo.ForumUser
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

ALTER TABLE dbo.TopicTrack ADD CONSTRAINT
	[FK_dbo.TopicTrack_dbo.Topic_Topic_Id] FOREIGN KEY
	(
	TopicId
	) REFERENCES dbo.Topic
	(
	Id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO

COMMIT


BEGIN TRANSACTION
GO

CREATE NONCLUSTERED INDEX IX_OriginalTopicId ON dbo.Topic
	(
	OriginalTopicId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

ALTER TABLE dbo.Topic SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

